# -*- coding: utf-8 -*-
from functools import partial
from common.lottery.cyclical.tj_ssc.model.order import BET_TYPE
from common.lottery.cyclical.cq_ssc.logic import order as base
from common.lottery.cyclical.ssc.order import (
    ssc_calc_total_price_decorator,
    ssc_valid_number_decorator,
    ssc_generate_win_func_decorator,
)

RATE_CONFIG = {         # base on UNIT==2
    BET_TYPE.ONE_SINGLE: 10,
    BET_TYPE.TWO_SINGLE: 98,
    BET_TYPE.THREE_SINGLE: 980,
    BET_TYPE.FIVE_SINGLE: 98000,
    BET_TYPE.TWO_COMBINE: 48,
    BET_TYPE.THREE_GROUP_SINGLE: 320,
    BET_TYPE.THREE_GROUP_MULTI: 320,
    BET_TYPE.THREE_GROUP_SIX: 160,
    BET_TYPE.FIVE_ALL: [20460, 250, 20],    # 全部，前后三，前后二
    BET_TYPE.TWO_BSOE: 4,
    BET_TYPE.TWO_SUM: 98,
    BET_TYPE.CHOSEN_ONE: 10,
    BET_TYPE.FOUR_SINGLE: 9800,
}

UNIT = 2


@ssc_valid_number_decorator
def valid_number(bet_type, number):
    if bet_type == BET_TYPE.FOUR_SINGLE:
        positions = number.split('|')
        assert len(positions) == 4
        for position in positions:
            nums = position.split(',')
            assert 10 >= len(nums) >= 1
    else:
        base.valid_number(bet_type, number)


@ssc_calc_total_price_decorator
def calc_total_price(bet_type, number, times=1, unit=UNIT):
    if bet_type == BET_TYPE.FOUR_SINGLE:
        positions = number.split('|')
        nums = []
        for position in positions:
            if not position:
                continue
            ds = position.split(',')
            nums.append(ds)
        price = 1
        for num in nums:
            price *= len(num)
        return price * times * unit
    else:
        return base.calc_total_price(bet_type, number, times, unit)


@ssc_generate_win_func_decorator
def generate_win_func(number):
    result = base.generate_win_func(number)

    def four_single(x):
        postions = x.split('|')
        is_in = True
        for i, v in enumerate(number[-4:]):
            if v not in postions[i]:
                is_in = False
                break
        return 1 if is_in else 0

    result[BET_TYPE.FOUR_SINGLE] = four_single
    return result


calc_win_price = partial(base.calc_win_price, rate_config=RATE_CONFIG)
